package com.qf.service.Impl;

import com.qf.feign.CacheService;
import com.qf.service.SendReportService;
import com.qianfeng.smsplatform.common.constants.CacheConstants;
import com.qianfeng.smsplatform.common.constants.RabbitMqConsants;
import com.qianfeng.smsplatform.common.model.Standard_Report;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/*
 * @Data: 2021/7/19 22:39
 * */
@Service
public class SendReportServiceImpl implements SendReportService {

   private RestTemplate restTemplate;
    private CacheService cacheService;
    @Autowired
    public void setCacheService(CacheService cacheService) {
        this.cacheService = cacheService;
    }
    @Autowired
    public void setRestTemplate(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    @Override
    public void sendReport(Standard_Report report) {
//    通知客户需要知道客户的路径   先查询客户的信息
        Map hmget = cacheService.hmget(CacheConstants.CACHE_PREFIX_CLIENT + report.getClientID());

      if(hmget!=null){
//          获取客户的路径  需要请求这个地址 发消息发送过去
          String receivestatusurl =(String) hmget.get("receivestatusurl");
          System.out.println("客户的路径为"+receivestatusurl);
          //我们约定我们会通过post请求的方式将参数传递过去,并且要求对方返回一个 ok字符串,返回其他任何内容我们都认为是失败
          String result = restTemplate.postForObject(receivestatusurl, report, String.class);
          System.out.println("客户返回的结果是"+result);
          if (!"ok".equals(result)) {
              report.setSendCount(2);
              //TODO 将当前的状态报告保存起来, 后面通过定时任务进行发送
          }

      }




    }
}
